﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuantitativeInvestment.Container;
using System.Threading;
using QuantitativeInvestment.Tools;
namespace QuantitativeInvestment.View
{
     partial class MainForm : Form
    {
        public Container.Container container;
    
        public MainForm()
        {
            InitializeComponent();
            loadToolStripMenuItems();
            this.container = QuantitativeInvestment.Container.Container.createContainerByName("简单容器");

            this.updateModelView();
            this.progressBar2.Value =(Int32)( this.container.progressValue);
        }

        public void loadToolStripMenuItems()
        {
           List<string> nameList=  Model.Model.getModelNameList();
           foreach (string modelName in nameList)
           {
               ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
               toolStripMenuItem.Name = modelName;
               toolStripMenuItem.Size = new System.Drawing.Size(41, 20);
               toolStripMenuItem.Text = modelName;
               toolStripMenuItem.Click += new System.EventHandler(this.modelToolStripMenuItems_Click);
               this.modelToolStripMenuItem.DropDownItems.Add(toolStripMenuItem);
           }

           List<string> poolNameList = StockPool.StockPool.getStockPoolNameList();
           foreach (string poolName in poolNameList)
           {
               ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
               toolStripMenuItem.Name = poolName;
               toolStripMenuItem.Size = new System.Drawing.Size(41, 20);
               toolStripMenuItem.Text = poolName;
               toolStripMenuItem.Click += new System.EventHandler(this.stockPoolToolStripMenuItems_Click);
               this.stockPoolToolStripMenuItem.DropDownItems.Add(toolStripMenuItem);
           }

        }
        private void modelToolStripMenuItems_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            Model.Model model = Model.Model.createModelByName(t.Name);
            model.container = this.container;
            this.container.modelList.Add(model);

            updateModelView();
        }

        private void stockPoolToolStripMenuItems_Click(object sender, EventArgs e)
        {


            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            StockPool.StockPool pool = StockPool.StockPool.createStockPoolByName(t.Name);

            this.container.poolList.Clear();
            this.container.poolList.Add(pool);
            updateModelView();
        }

        public  void updateModelView()
        {
            //更新数据源列表
            this.tabPage1.Controls.Clear();
           
            int position = 56;
            if (this.container.poolList.Count > 0)
            {
                PictureBox pictureBox1 = new PictureBox();
                this.tabPage1.Controls.Add(pictureBox1);
                pictureBox1.Image = global::QuantitativeInvestment.Properties.Resources.dataSource;
                pictureBox1.InitialImage = global::QuantitativeInvestment.Properties.Resources.dataSource;
                pictureBox1.Location = new System.Drawing.Point(position, 89);
                pictureBox1.Size = new System.Drawing.Size(50, 44);
                pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                pictureBox1.TabIndex = 0;
                pictureBox1.TabStop = false;
                pictureBox1.Paint += new PaintEventHandler(this.pictureBox_Paint);
                pictureBox1.Name = this.container.poolList[0].poolName;
                pictureBox1.MouseDoubleClick += new MouseEventHandler(sender_StockPoolPicBoxMouseDoubleClick);
                pictureBox1.Refresh();
            }
            position = position + 70;
            //更新模型列表
            for (int i=0;i<this.container.modelList.Count;i++)
            {
                Model.Model model = this.container.modelList[i];
                //画一个箭头
                PictureBox pictureBox4 = new PictureBox();
                this.tabPage1.Controls.Add(pictureBox4);
                pictureBox4.Image = global::QuantitativeInvestment.Properties.Resources.arrow;
                pictureBox4.InitialImage = global::QuantitativeInvestment.Properties.Resources.arrow;
                pictureBox4.Location = new System.Drawing.Point(position-20, 100);
                pictureBox4.Size = new System.Drawing.Size(20, 20);
                pictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                pictureBox4.TabIndex = 0;
                pictureBox4.TabStop = false;
                pictureBox4.Refresh();

                //模型
                PictureBox pictureBox2 = new PictureBox();
                this.tabPage1.Controls.Add(pictureBox2);
                pictureBox2.Image = global::QuantitativeInvestment.Properties.Resources.model;
                pictureBox2.InitialImage = global::QuantitativeInvestment.Properties.Resources.model;
                pictureBox2.Location = new System.Drawing.Point(position, 89);
                pictureBox2.Size = new System.Drawing.Size(99, 44);
                pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                pictureBox2.TabIndex = i;
                pictureBox2.TabStop = false;
                pictureBox2.Paint += new PaintEventHandler(this.pictureBox_Paint);
                pictureBox2.MouseDoubleClick += new MouseEventHandler(sender_ModelPicBoxMouseDoubleClick);
                pictureBox2.Name = model.name;
                pictureBox2.Refresh();
                position = position + 120;
             
            }


            //画一个箭头
            PictureBox pictureBox5 = new PictureBox();
            this.tabPage1.Controls.Add(pictureBox5);
            pictureBox5.Image = global::QuantitativeInvestment.Properties.Resources.arrow;
            pictureBox5.InitialImage = global::QuantitativeInvestment.Properties.Resources.arrow;
            pictureBox5.Location = new System.Drawing.Point(position - 20, 100);
            pictureBox5.Size = new System.Drawing.Size(20, 20);
            pictureBox5.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            pictureBox5.TabIndex = 0;
            pictureBox5.TabStop = false;
            pictureBox5.Refresh();

            //结束
            PictureBox pictureBox3= new PictureBox();
            this.tabPage1.Controls.Add(pictureBox3);
            pictureBox3.Image = global::QuantitativeInvestment.Properties.Resources.end;
            pictureBox3.InitialImage = global::QuantitativeInvestment.Properties.Resources.end;
            pictureBox3.Location = new System.Drawing.Point(position, 89);
            pictureBox3.Size = new System.Drawing.Size(40, 44);
            pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            pictureBox3.TabIndex = 0;
            pictureBox3.TabStop = false;
            pictureBox3.Name = "容器";
            pictureBox3.Paint += new PaintEventHandler(this.pictureBox_Paint);
            pictureBox3.MouseDoubleClick += new MouseEventHandler(sender_ContainerPicBoxMouseDoubleClick);
            pictureBox3.Refresh();
            position = position + 64;





            //特征分析

            //画一个箭头
            PictureBox pictureBox7= new PictureBox();
            this.tabPage1.Controls.Add(pictureBox7);
            pictureBox7.Image = global::QuantitativeInvestment.Properties.Resources.arrow;
            pictureBox7.InitialImage = global::QuantitativeInvestment.Properties.Resources.arrow;
            pictureBox7.Location = new System.Drawing.Point(position - 20, 100);
            pictureBox7.Size = new System.Drawing.Size(20, 20);
            pictureBox7.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            pictureBox7.TabIndex = 0;
            pictureBox7.TabStop = false;
            pictureBox7.Refresh();


            PictureBox pictureBox6= new PictureBox();
            this.tabPage1.Controls.Add(pictureBox6);
            pictureBox6.Image = global::QuantitativeInvestment.Properties.Resources.Analyser;
            pictureBox6.InitialImage = global::QuantitativeInvestment.Properties.Resources.Analyser;
            pictureBox6.Location = new System.Drawing.Point(position, 89);
            pictureBox6.Size = new System.Drawing.Size(40, 44);
            pictureBox6.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            pictureBox6.TabIndex = 0;
            pictureBox6.TabStop = false;
            pictureBox6.Name = "特征";
            pictureBox6.Paint += new PaintEventHandler(this.pictureBox_Paint);
            pictureBox6.MouseDoubleClick += new MouseEventHandler(sender_AnalyerPicBoxMouseDoubleClick);
            pictureBox6.Refresh();
           



            this.tabPage1.Controls.Add(this.progressBar2);
            this.progressBar2.Value = 0;




            this.tabPage1.Refresh();

        }

       //控制模型移动的代码

        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            PictureBox t = (PictureBox)sender;
            e.Graphics.DrawString(t.Name, this.Font, Brushes.Red, new PointF(10, 10));
        }

        private void sender_ModelPicBoxMouseDoubleClick(object sender, MouseEventArgs e)
        {

            PictureBox s = (PictureBox)sender;
            ModelForm modelForm = new ModelForm(this.container.modelList[s.TabIndex], this, false);
            modelForm.Show();
        }

        private void sender_ContainerPicBoxMouseDoubleClick(object sender, MouseEventArgs e)
        {
            ContainerForm containerForm = new ContainerForm( this);
            containerForm.Show();
        }

        private void sender_AnalyerPicBoxMouseDoubleClick(object sender, MouseEventArgs e)
        {
            AnalyserForm analyserForm = new AnalyserForm(this);
            analyserForm.Show();
        }

        private void sender_StockPoolPicBoxMouseDoubleClick(object sender, MouseEventArgs e)
        {
            StockPoolForm poolForm = new StockPoolForm(this,this.container.poolList[0]);
            poolForm.Show();
        }

        private void toolStripButtonStart_Click(object sender, EventArgs e)
        {
            this.container.stocks.Clear();

            if (this.普通模式ToolStripMenuItem.Checked)
            {
                this.container.isTestModel = false;
                Thread t = new Thread(new ThreadStart(this.container.run));
                t.Start();
                timer1.Enabled = true;
            }
            else if (this.参数测试模式ToolStripMenuItem.Checked)
            {
                this.container.isTestModel = true;
                Thread t = new Thread(new ThreadStart(this.container.testModeRun));
                t.Start();
                timer1.Enabled = true;

            }

         //   this.container.caculateStatistics();
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            this.container.modelList.Clear();
            this.container.stocks.Clear();
            this.updateModelView();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.progressBar2.Value = (Int32)this.container.progressValue;
            if (this.progressBar2.Value == 100 && !this.container.isTestModel )
            {
                timer1.Enabled = false;
                MessageBox.Show("计算完成！");
            }
            else if (this.progressBar2.Value == 100 && this.container.isTestModel)
            {
                timer1.Enabled = false;
                MessageBox.Show("计算完成！");
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
                        //打开对话框
            OpenFileDialog otext=new OpenFileDialog();
            otext.InitialDirectory = "My Models";
            //检查文件是否存在，路径是否正确
            otext.CheckFileExists=true;
            otext.CheckPathExists=true;
            //过滤文件类型，后缀名只为models的文件
            otext.Filter = "文本文件(*.models)|*.models";
            //如果确定则有如下代码
            otext.CheckFileExists=false;
            if(otext.ShowDialog()==DialogResult.OK)
            {

                XmlDataSource xmlData = new XmlDataSource();
                xmlData.ouputContainer(this.container,otext.FileName );
                
            }
            otext.InitialDirectory = "";
        
        }

        private void 载入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //打开对话框
            OpenFileDialog otext = new OpenFileDialog();
            //检查文件是否存在，路径是否正确
            otext.CheckFileExists = true;
            otext.CheckPathExists = true;
            //过滤文件类型，后缀名只为models的文件
            otext.Filter = "文本文件(*.models)|*.models";
            //如果确定则有如下代码
            if (otext.ShowDialog() == DialogResult.OK)
            {
                XmlDataSource xmlDataSource = new XmlDataSource();
                this.container= xmlDataSource.readContainer(otext.FileName);
                
            }
            updateModelView();
        }

        private void toolStripButtonLatestRecommendation_Click(object sender, EventArgs e)
        {
            this.container.stocks.Clear();
            Thread t = new Thread(new ThreadStart(this.container.lastestPortfolios));
            t.Start();
            timer1.Enabled = true;
       
        }

        private void 普通模式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.普通模式ToolStripMenuItem.Checked = true;
            this.参数测试模式ToolStripMenuItem.Checked = false;
        }

        private void 参数测试模式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.普通模式ToolStripMenuItem.Checked = false;
            this.参数测试模式ToolStripMenuItem.Checked = true;
        }


      

    }
}
